package com.zwjstudy.cachestudy.redis7.lua;

import redis.clients.jedis.Jedis;

/**
 * ClassName: JedisLuaExample
 * Package: com.zwjstudy.cachestudy.redis7.lua
 * Description:
 *
 * @Author: 张文俊
 * @Create: 2025-09-23 - 9:15
 * @Version: v1.0
 */
public class JedisLuaExample {

    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost", 6379);
        jedis.auth("123456");

        String luaScript = "local key = KEYS[1] " +
                "local current = redis.call('GET', key) " +
                "if current == false then " +
                "    redis.call('SET', key, 1) " +
                "    return 1 " +
                "else " +
                "    redis.call('INCR', key) " +
                "    return tonumber(current) + 1 " +
                "end";

        // 执行脚本，1表示KEYS数组的数量，后面是key和参数
        Object result = jedis.eval(luaScript, 1, "myCounter");
        System.out.println("Result: " + result);

        jedis.close();
    }

}
